<html>
<head>
<title>switchPlatfrom.py</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.s0 { color: #75715e;}
.s1 { color: #f8f8f2;}
.s2 { color: #66d9ef; font-style: italic;}
.s3 { color: #f92672;}
.s4 { color: #e6db74;}
.s5 { color: #ae81ff;}
.ln { color: #f8f8f2; font-weight: normal; font-style: normal; }
</style>
</head>
<body bgcolor="#272822">
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060" >
<tr><td><center>
<font face="Arial, Helvetica" color="#000000">
switchPlatfrom.py</font>
</center></td></tr></table>
<pre><a name="l1"><span class="ln">1    </span></a><span class="s0">#!/usr/bin/python3</span>
<a name="l2"><span class="ln">2    </span></a><span class="s0"># -*- coding: utf-8 -*-</span>
<a name="l3"><span class="ln">3    </span></a><span class="s0">&quot;&quot;&quot; 
<a name="l4"><span class="ln">4    </span></a>@File Name  : switchPlatfrom.py 
<a name="l5"><span class="ln">5    </span></a>@Author     : LeeCQ 
<a name="l6"><span class="ln">6    </span></a>@Date-Time  : 2020/8/29 11:18 
<a name="l7"><span class="ln">7    </span></a> 
<a name="l8"><span class="ln">8    </span></a>切换平台 
<a name="l9"><span class="ln">9    </span></a>&quot;&quot;&quot;</span>
<a name="l10"><span class="ln">10   </span></a><span class="s2">from </span><span class="s1">login </span><span class="s2">import </span><span class="s1">login_easy</span>
<a name="l11"><span class="ln">11   </span></a><span class="s2">from </span><span class="s1">common </span><span class="s2">import </span><span class="s3">*</span>
<a name="l12"><span class="ln">12   </span></a><span class="s2">from </span><span class="s1">exc </span><span class="s2">import </span><span class="s1">ElementNotFind, NotLoginError</span>
<a name="l13"><span class="ln">13   </span></a><span class="s2">import </span><span class="s1">unittest, time</span>
<a name="l14"><span class="ln">14   </span></a>
<a name="l15"><span class="ln">15   </span></a>
<a name="l16"><span class="ln">16   </span></a><span class="s2">class </span><span class="s1">TestPlat(unittest.TestCase)</span><span class="s3">:</span>
<a name="l17"><span class="ln">17   </span></a>    <span class="s0">&quot;&quot;&quot;&quot;&quot;&quot;</span>
<a name="l18"><span class="ln">18   </span></a>
<a name="l19"><span class="ln">19   </span></a>    <span class="s2">def </span><span class="s1">setUp(self) -&gt; </span><span class="s2">None</span><span class="s3">:</span>
<a name="l20"><span class="ln">20   </span></a>        <span class="s1">self.driver </span><span class="s3">= </span><span class="s1">login_easy(</span><span class="s4">'test'</span><span class="s1">, </span><span class="s4">'123456'</span><span class="s1">)</span>
<a name="l21"><span class="ln">21   </span></a>        <span class="s1">self.plat </span><span class="s3">= </span><span class="s4">'自动测试机构'</span>
<a name="l22"><span class="ln">22   </span></a>
<a name="l23"><span class="ln">23   </span></a>    <span class="s2">def </span><span class="s1">tearDown(self) -&gt; </span><span class="s2">None</span><span class="s3">:</span>
<a name="l24"><span class="ln">24   </span></a>        <span class="s1">self.driver.close()</span>
<a name="l25"><span class="ln">25   </span></a>
<a name="l26"><span class="ln">26   </span></a>    <span class="s2">def </span><span class="s1">test_plat_from(self)</span><span class="s3">:</span>
<a name="l27"><span class="ln">27   </span></a>        <span class="s1">d </span><span class="s3">= </span><span class="s1">switch_plat_from(self.driver, self.plat)</span>
<a name="l28"><span class="ln">28   </span></a>        <span class="s1">self.assertEqual(d.find_element_by_css_selector(</span><span class="s4">'#main-hd-title &gt; a:nth-child(2)'</span><span class="s1">).text.strip(),</span>
<a name="l29"><span class="ln">29   </span></a>                         <span class="s1">self.plat,</span>
<a name="l30"><span class="ln">30   </span></a>                         <span class="s4">'切换平台成功'</span>
<a name="l31"><span class="ln">31   </span></a>                         <span class="s1">)</span>
<a name="l32"><span class="ln">32   </span></a>
<a name="l33"><span class="ln">33   </span></a>
<a name="l34"><span class="ln">34   </span></a><span class="s2">def </span><span class="s1">switch_plat_from(_driver</span><span class="s3">: </span><span class="s1">BaseWebDriver, plat_name)</span><span class="s3">:</span>
<a name="l35"><span class="ln">35   </span></a>    <span class="s0">&quot;&quot;&quot;已经登录的WebDriver对象 
<a name="l36"><span class="ln">36   </span></a> 
<a name="l37"><span class="ln">37   </span></a>    :param _driver: 
<a name="l38"><span class="ln">38   </span></a>    :param plat_name: 需要跳转到的机构名 
<a name="l39"><span class="ln">39   </span></a>    :return: 跳转到 {plat_name} webDriver 
<a name="l40"><span class="ln">40   </span></a>    &quot;&quot;&quot;</span>
<a name="l41"><span class="ln">41   </span></a>    <span class="s1">_driver.implicitly_wait(</span><span class="s5">8</span><span class="s1">)</span>
<a name="l42"><span class="ln">42   </span></a>
<a name="l43"><span class="ln">43   </span></a>    <span class="s2">if </span><span class="s1">_is_login(_driver) </span><span class="s2">is False</span><span class="s3">:</span>
<a name="l44"><span class="ln">44   </span></a>        <span class="s2">raise </span><span class="s1">NotLoginError(</span><span class="s4">'检查登录状态'</span><span class="s1">)</span>
<a name="l45"><span class="ln">45   </span></a>
<a name="l46"><span class="ln">46   </span></a>    <span class="s1">_driver.find_element_by_css_selector(</span><span class="s4">'#topnav &gt; li:nth-child(3)'</span><span class="s1">).click()</span>
<a name="l47"><span class="ln">47   </span></a>    <span class="s1">_driver.switch_to.frame(_driver.find_element_by_id(</span><span class="s4">'get'</span><span class="s1">))</span>
<a name="l48"><span class="ln">48   </span></a>    <span class="s1">_driver.find_element_by_css_selector(</span><span class="s4">'#name &gt; div'</span><span class="s1">).click()</span>
<a name="l49"><span class="ln">49   </span></a>    <span class="s1">_driver.find_element_by_class_name(</span><span class="s4">'bbit-tree-root'</span><span class="s1">).find_element_by_xpath(</span><span class="s4">f'//*[@title=&quot;</span><span class="s5">{</span><span class="s1">plat_name</span><span class="s5">}</span><span class="s4">&quot;]'</span><span class="s1">).click()</span>
<a name="l50"><span class="ln">50   </span></a>    <span class="s1">_driver.switch_to.default_content()</span>
<a name="l51"><span class="ln">51   </span></a>    <span class="s1">_driver.find_element_by_class_name(</span><span class="s4">'layui-layer-btn0'</span><span class="s1">).click()</span>
<a name="l52"><span class="ln">52   </span></a>    <span class="s1">time.sleep(</span><span class="s5">1</span><span class="s1">)  </span><span class="s0"># 需要强制等待页面跳转</span>
<a name="l53"><span class="ln">53   </span></a>    <span class="s2">return </span><span class="s1">_driver</span>
<a name="l54"><span class="ln">54   </span></a>
<a name="l55"><span class="ln">55   </span></a>
<a name="l56"><span class="ln">56   </span></a><span class="s2">def </span><span class="s1">_is_login(_driver</span><span class="s3">: </span><span class="s1">BaseWebDriver)</span><span class="s3">:</span>
<a name="l57"><span class="ln">57   </span></a>    <span class="s0">&quot;&quot;&quot;判断是否登录&quot;&quot;&quot;</span>
<a name="l58"><span class="ln">58   </span></a>    <span class="s2">try</span><span class="s3">:</span>
<a name="l59"><span class="ln">59   </span></a>        <span class="s1">_driver.find_element_by_id(</span><span class="s4">'topnav'</span><span class="s1">)</span>
<a name="l60"><span class="ln">60   </span></a>        <span class="s2">return True</span>
<a name="l61"><span class="ln">61   </span></a>    <span class="s2">except </span><span class="s1">ElementNotFind</span><span class="s3">:</span>
<a name="l62"><span class="ln">62   </span></a>        <span class="s2">return False</span>
<a name="l63"><span class="ln">63   </span></a>
<a name="l64"><span class="ln">64   </span></a>
<a name="l65"><span class="ln">65   </span></a><span class="s2">if </span><span class="s1">__name__ </span><span class="s3">== </span><span class="s4">'__main__'</span><span class="s3">:</span>
<a name="l66"><span class="ln">66   </span></a>    <span class="s1">unittest.main()</span>
<a name="l67"><span class="ln">67   </span></a></pre>
</body>
</html>